#version 450 core

layout (location = 0) out vec4 o_Color;
//out int o_EntityID;

struct VertexOutput
{
	vec4 Color;
	vec2 TexCoord;
	float TilingFactor;
	vec2 UV;
};

layout (location = 0) in VertexOutput Output;
layout (location = 4) in float v_TexIndex;
layout (location = 5) in flat int v_EntityID;

uniform mediump vec2 TextureSize0;
uniform lowp float BlurRadius; 
uniform mediump vec2 BlurDirection;

uniform sampler2D u_Textures[5];

vec4 gaussianBlur(mediump vec2 coord, lowp vec2 dir, sampler2D colorTexture)
{
    // Defines the one-dimensional Gaussian Kernel with 9 samples.
    float GAUSSIAN_KERNEL[9];
    GAUSSIAN_KERNEL[0] = 0.028532;
    GAUSSIAN_KERNEL[1] = 0.067234;
    GAUSSIAN_KERNEL[2] = 0.124009;
    GAUSSIAN_KERNEL[3] = 0.179044;
    GAUSSIAN_KERNEL[4] = 0.20236;
    GAUSSIAN_KERNEL[5] = 0.179044;
    GAUSSIAN_KERNEL[6] = 0.124009;
    GAUSSIAN_KERNEL[7] = 0.067234;
    GAUSSIAN_KERNEL[8] = 0.028532;

    vec2 texel = 1.0/TextureSize0;
    vec4 sum = vec4(0.0);
    // Get the original texture coordinate for this fragment.
    vec2 tc = coord;
    // Get the amount to blur.
    float blur = BlurRadius;
    // Set the amount of blur in the horizontal direction.
    float hstep = dir.x*texel.x;
    // Set the amount of blur in the vertical direction.
    float vstep = dir.y*texel.y;

    // Sample the texture 9 times for every fragment.
    for(int i = 0; i < 9; i++)
    {
        float pixelOffset = (float(i) - floor(9.0 * 0.5));
        mediump vec2 coord = vec2(tc.x + pixelOffset * blur * hstep, tc.y + pixelOffset * blur * vstep);
        sum += texture2D(colorTexture, coord) * GAUSSIAN_KERNEL[i];
    }
    //sum = texture2D(colorTexture, coord);

    return sum;
}

void main()
{
	vec4 texColor = Output.Color;
    vec2 uv = Output.TexCoord * Output.TilingFactor;
    uv.y = 1.0 -uv.y;

	switch(int(v_TexIndex))
	{
		//case  0: texColor *= gaussianBlur(uv, BlurDirection, u_Textures[0]); break;
		case  0: texColor *= texture(u_Textures[0], uv); break;
		case  1: texColor *= texture(u_Textures[1], uv); break;
		case  2: texColor *= texture(u_Textures[2], uv); break;
		case  3: texColor *= texture(u_Textures[3], uv); break;
		case  4: texColor *= texture(u_Textures[4], uv); break;
	}
    //texColor+= vec4(0.1, 0.1, 0.0, 1.0);
	if (texColor.a == 0.0)
		discard;

	o_Color = texColor;
	//float depthValue = texture(u_Textures[0], Output.TexCoord).z;
	//o_Color = vec4(vec3(depthValue), 1.0);
	//o_EntityID = v_EntityID;
}
